package mediadisplay.timeline 
{
	import flash.display.BlendMode;
	import flash.display.GradientType;
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.geom.Matrix;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	import mediadisplay.ui.DeleteIcon;
	/**
	 * ...
	 * @author ...
	 */
	public class TimelineFocusedMarker extends Sprite
	{
		private var _color:uint;
		public var start:Number;
		public var stop:Number;
		public var content:String;
		
		public var tf:TextField;
		public var textformat:TextFormat;
		public var xPos:Number;
		public var length:Number = 5;
		public var overviewMarker:TimelineOverviewMarker;
		public var index:int;
		public var ignore:Boolean;
		private var deleteIcon:DeleteIcon;
		
		public static const DELETE_MARKER:String = "deleteThisMarker";
		
		public function TimelineFocusedMarker(index:int, length:Number, content:String, start:Number, stop:Number, color:uint, ignore:Boolean) 
		{
			this.index = index;
			this.length = length;
			this.content = content;
			this.start = start;
			this.stop = stop;
			this.ignore = ignore;
			_color = color;
			
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			textformat = new TextFormat();
			textformat.color = 0;
			textformat.size = 12;
			
			tf = new TextField();
			addChild(tf);
			tf.x = 3;
			tf.y = 12;
			tf.width = 0;
			tf.height = 20;
			tf.defaultTextFormat = textformat;
			tf.type = TextFieldType.DYNAMIC;
			tf.selectable = false;
			tf.multiline = false;
			tf.wordWrap = false;
			tf.filters = [new GlowFilter(0xffffff, 1, 1, 1, 2, 1)];
			tf.text = content;
			tf.mouseEnabled = false;
			
			draw(length);
			
			deleteIcon = new DeleteIcon();
			addChild(deleteIcon);
			deleteIcon.x = deleteIcon.y = 1;
			deleteIcon.visible = false;
			deleteIcon.addEventListener(MouseEvent.CLICK, deleteMarker);
			deleteIcon.mouseEnabled = true;
			
			stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
			stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
			addEventListener(Event.REMOVED_FROM_STAGE, cleanup);			
		}
		
		private function keyDownHandler(e:KeyboardEvent):void 
		{
			if (e.shiftKey) {
				deleteIcon.visible = true;
				addChild(deleteIcon);
			}
		}
		
		public function updateText():void {
			tf.text = content;
			tf.width = Math.max(0, length - 5);
			tf.setTextFormat(textformat);		}
		
		private function keyUpHandler(e:KeyboardEvent):void 
		{
			deleteIcon.visible = false;
		}
		
		private function cleanup(e:Event):void 
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, cleanup);
			deleteIcon.removeEventListener(MouseEvent.CLICK, deleteMarker);
			stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
			stage.removeEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
		}
		
		private function deleteMarker(e:MouseEvent):void 
		{
			trace("delete marker " + index);
			dispatchEvent(new Event(DELETE_MARKER));
		}
		
		public function draw(length:Number):void 
		{
			updateText();
			
			var fType:String = GradientType.LINEAR;
			var colors:Array = [ _color, 0 ];
			var alphas:Array = [ 1, 1 ];
			var ratios:Array = [ 0, 255 ];
			var matr:Matrix = new Matrix();
			matr.createGradientBox( 90, 10, 0, 0, 0 );
			matr.rotate(Math.PI/2);
			var sprMethod:String = SpreadMethod.PAD;
			
			graphics.clear();
			graphics.lineStyle(1, 0x666666);
			graphics.beginGradientFill( fType, colors, alphas, ratios, matr, sprMethod );
			graphics.drawRect(0, 0, length, 32);
			graphics.endFill();	
			
			graphics.lineStyle(0);
			graphics.moveTo(0, 0);
			graphics.beginFill(0);
			graphics.drawRect(0, 0, 2, 32);
			graphics.endFill();
			
			graphics.beginFill(0);
			graphics.drawRect(length - 2, 0, 2, 32);
			graphics.endFill();
		}
		
	}

}